package xin.printk.android.module.register;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Locale;

import xin.printk.android.BasicFragment;
import xin.printk.android.R;
import xin.printk.android.data.event.LoginEvent;
import xin.printk.android.utils.LogUtils;
import xin.printk.android.utils.NumberUtils;

public class RegisterFragment extends BasicFragment implements RegisterContract.View {

    private Button mGetVerifyCodeButton;
    private String mVerifyCode = "unknown";

    private Button mRegisterButton;
    private EditText mAccountText;
    private EditText mPasswordText;
    private EditText mVerifyCodeText;

    private RegisterContract.Presenter mPresenter;

    private CountDownTimer mCountDownTimer = new CountDownTimer(1000 * 60, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            mGetVerifyCodeButton.setText(String.format(Locale.getDefault(), "%d秒后重新发送", millisUntilFinished / 1000));
        }

        @Override
        public void onFinish() {
            mVerifyCode = "unknown";
            mGetVerifyCodeButton.setEnabled(true);
            mGetVerifyCodeButton.setText(getString(R.string.retry_get_verity_code));
        }
    };

    @Override
    protected int bindLayoutResId() {
        return R.layout.fragment_register;
    }

    @Override
    protected void onInit() {
        mGetVerifyCodeButton = findView(R.id.bt_get_verify_code);
        mAccountText = findView(R.id.et_register_account);
        mPasswordText = findView(R.id.et_register_password);
        mVerifyCodeText = findView(R.id.et_register_verify_code);
        mRegisterButton = findView(R.id.bt_register);
    }

    @Override
    protected void onExecute(Bundle savedInstanceState) {
        mGetVerifyCodeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.getVerifyCode();
            }
        });
        mRegisterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.register();
            }
        });
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = new RegisterPresenter(this);
        if (!EventBus.getDefault().isRegistered(this)) {
            LogUtils.d("RegisterFragment register EventBus");
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
            mCountDownTimer = null;
        }
        if (EventBus.getDefault().isRegistered(this)) {
            LogUtils.d("RegisterFragment unregister EventBus");
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    public String getAccount() {
        return mAccountText.getText().toString();
    }

    @Override
    public String getPassword() {
        return mPasswordText.getText().toString();
    }

    @Override
    public boolean isVerifyCodeValid() {
        return mVerifyCodeText.getText().toString().equals(mVerifyCode);
    }

    @Override
    public void mockVerifyCode() {
        mGetVerifyCodeButton.setEnabled(false);
        mCountDownTimer.start();
        mVerifyCode = NumberUtils.generateVerifyCode(6);
        Snackbar.make(mGetVerifyCodeButton, getString(R.string.get_verity_code_tips) + mVerifyCode, Snackbar.LENGTH_LONG).show();
    }

    @Override
    public void showMessage(String message) {
        Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
    }

    @Subscribe()
    public void onEvent(LoginEvent event) {
        LogUtils.d("register receive event");
        if (event.isOnline()) {
            getActivity().finish();
        }
    }

    @Override
    public void registerSuccess() {
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
            mCountDownTimer = null;
        }
        EventBus.getDefault().post(new LoginEvent(true));
    }
}
